package cn.calm.xhtml;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.ElementChangedEvent;
import org.eclipse.jdt.core.IElementChangedListener;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.osgi.framework.BundleContext;

import cn.calm.xhtml.entity.Tag;
import cn.calm.xhtml.handler.NamespaceHandler;
import cn.calm.xhtml.listener.ProjectListener;
import cn.calm.xhtml.nature.XhtmlNature;

/**
 * The activator class controls the plug-in life cycle
 */
public class XhtmlActivator extends AbstractUIPlugin implements
		org.eclipse.ui.IStartup {

	// The plug-in ID
	public static final String PLUGIN_ID = "cn.calm.xhtml.plugin"; //$NON-NLS-1$
	// The shared instance
	private static XhtmlActivator plugin;
	private static Map<IProject, Map<String,Set<Tag>>> namespaces = new HashMap<>();

	/**
	 * The constructor
	 */
	public XhtmlActivator() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		logInfo("start");
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		workspace.addResourceChangeListener(new ProjectListener(),
				IResourceChangeEvent.POST_CHANGE);
		IProject[] projects = workspace.getRoot().getProjects();
		if (projects != null) {
			for (IProject project : projects) {
				if(project.isOpen()){
					if (project.hasNature(JavaCore.NATURE_ID)
							&& project.hasNature(XhtmlNature.XHTML_NATURE_ID)) {
						NamespaceHandler.parseProject(JavaCore.create(project));
					}
				}else{
					logInfo(project+"is not open");
				}
			}
		}
		IElementChangedListener listener = new IElementChangedListener() {
			public void elementChanged(final ElementChangedEvent event) {
				System.out.println(event);
			}
		};

		JavaCore.addElementChangedListener(listener,
				ElementChangedEvent.POST_CHANGE);
	}

	/**
	 * Find the Eclipse project for the file the user is working on.
	 * 
	 * @return The project owning the file the user has open.
	 */
	public static IJavaProject findCurrentJavaProject() {

		StructuredTextEditor editor = (StructuredTextEditor)getDefault().getWorkbench()
				.getWorkbenchWindows()[0].getActivePage().getActiveEditor();
		IEditorPart editorPart = editor.getEditorPart();
		FileEditorInput editorInput = (FileEditorInput) editorPart.getEditorInput();
		
		IFile file = editorInput.getFile();
		IProject project = file.getProject();
		if (isJavaProject(project)) {
			return JavaCore.create(project);
		}
		return null;
	}
	/**
	 * Check if the given project is a Java project.
	 * 
	 * @param project
	 * @return <tt>true</tt> if the project is a Java project.
	 */
	public static boolean isJavaProject(IProject project) {

		try {
			return project.isNatureEnabled(JavaCore.NATURE_ID);
		}
		catch (CoreException ex) {
			logError("Project not open, or doesn't exist", ex);
		}
		return false;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static XhtmlActivator getDefault() {
		return plugin;
	}

	public static void logInfo(String message) {
		plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, message));
	}

	public static void logError(String message) {
		plugin.getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, message));
	}

	public static void logError(String message, Throwable t) {
		plugin.getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, message, t));
	}

	public static Map<IProject, Map<String, Set<Tag>>> getNamespaces() {
		return namespaces;
	}

	@Override
	public void earlyStartup() {
		// ��ʱû��ʲô���ܣ�������Ϊ����eclipseһ������
	}
}
